package com.project.xsf.service.impl;

import com.project.junM.entity.TMember;
import com.project.xsf.entity.TAssessAnswer;
import com.project.xsf.entity.TAssessQuestion;
import com.project.xsf.entity.TMentalityResult;
import com.project.xsf.mapper.TAssessAnswerMapper;
import com.project.xsf.mapper.TAssessQuestionOptionMapper;
import com.project.xsf.mapper.TMentalityResultMapper;
import com.project.xsf.service.TMentalityResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Transactional
@Service
public class TMentalityResultImpl implements TMentalityResultService {
    @Autowired
    private TMentalityResultMapper tMentalityResultMapper;
    @Autowired
    private TAssessQuestionOptionMapper tAssessQuestionOptionMapper;
    @Autowired
    private TAssessAnswerMapper tAssessAnswerMapper;
    @Override
    public Boolean queryByMemberId(HttpSession session) {
        TMember member = (TMember) session.getAttribute("user");
        TMentalityResult tMentalityResult = tMentalityResultMapper.queryByMemberId(member.getId());
        //tMentalityResult为空表示没有评估过，tMentalityResult的评估建议有值表示评估完成可以重新评估
        if(tMentalityResult==null||tMentalityResult.getEvaluateId()!=null){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public Integer addTMentalityResult(TAssessQuestion[][] objects, HttpSession session) {
        TMember member = (TMember) session.getAttribute("user");
        TMentalityResult tMentalityResult = new TMentalityResult();
        tMentalityResult.setTMemberId(member.getId());
        tMentalityResult.setName(member.getName());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tMentalityResult.setCreatetime(simpleDateFormat.format(new Date()));
        for(int i=0;i<objects.length;i++){
            Integer markTotal=0;

            TAssessQuestion[] tAssessQuestions=(TAssessQuestion[])objects[i];
            for(int j=0;j<tAssessQuestions.length;j++){
//                System.out.println(tAssessQuestions[j]);
                TAssessQuestion tAssessQuestion = tAssessQuestions[j];
                TAssessAnswer tAssessAnswer = new TAssessAnswer();
                tAssessAnswer.setUserId(member.getId());
                tAssessAnswer.setQuestionId(tAssessQuestion.getId());
//                tAssessAnswer.setAnswerId();
                //设置答案id
                if(tAssessQuestion.getIsRadio().equals("0")){
                    tAssessAnswer.setAnswerId(tAssessQuestion.getSelectOption()+"");
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark();
                }else{
                    String options="";
                    for(int k=0;k<tAssessQuestion.getCheckOptions().size();k++){
                        options+=tAssessQuestion.getCheckOptions().get(k);
                        Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(k).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                        markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                        if(k<tAssessQuestion.getCheckOptions().size()-1){
                            options+=",";
                        }
                    }
                    tAssessAnswer.setAnswerId(options);
                }
                tAssessAnswerMapper.insertSelective(tAssessAnswer);
            }

            if(tAssessQuestions[0].getBankId()==13){
                //强迫症
                tMentalityResult.setTOcd(markTotal);
            }else if(tAssessQuestions[0].getBankId()==14){
                //焦虑
                tMentalityResult.setTAnxiety(markTotal);
            }else if(tAssessQuestions[0].getBankId()==15){
                //恐怖
                tMentalityResult.setTTerror(markTotal);
            }
        }
        System.out.println(tMentalityResult);
        return tMentalityResultMapper.insertSelective(tMentalityResult);
    }
}
